<?php
/**
 * 广告管理
 *
 *
 *
 *
 * @copyright  Copyright (c) 2007-2013 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */
defined('InShopNC') or exit('Access Invalid!');

class franchiseControl extends SystemControl{
	public function __construct(){
		parent::__construct();
	}

	/**
	 * 获取加盟商模型
	 * @return franchiserModel
	 */
	protected function getModelFranchise(){
		return Model('franchiser');
	}
	public function indexOp(){
		$franchise=$this->getModelFranchise();
		//分页
		$page	= new Page();
		$page->setEachNum(10);
		$page->setStyle('admin');
		$map=array();
		if(isset($_GET['search_title'])){
			$search_title=$_GET['search_title'];
			$map['franchisee_name']=array('like','%'.$search_title.'%');
			Tpl::output('search_title',$search_title);
		}
		$list=$franchise->getShowList($map,'*',$page);
		$count=Db::getLastCount();
		$page->setTotalNum($count);

		Tpl::output('page',$page->show());
		Tpl::output('list',$list);
		Tpl::showpage('franchise.index');
	}

	/**
	 * 加盟商文章分类id
	 * @var int
	 */
	protected $franchiser_ac_id=15;

	/**
	 * 保存加盟商品信息
	 * @return bool
	 */
	protected function save(){
		$franchise=$this->getModelFranchise();
		$field=array(
			'franchisee_id'=>1,
			'mapPoint'=>1,
			'areaid_1'=>1,
			'areaid_2'=>1,
			'franchisee_name'=>1,
			'franchisee_address'=>1,
			'linkname'=>1,
			'linkname_mobile'=>1,
		);
		$data=array_intersect_key($_POST,$field);
		if(count($data)!=count($field)){return false;}
		if($data['franchisee_id']){
			$franchise->table('franchiser')->where(array('franchisee_id'=>$data['franchisee_id']))->update($data);
			header("location:index.php?act=franchise&op=index");
		}else{
			$articleData=array(
				'ac_id'=>$this->franchiser_ac_id,//文章分类id,
				'article_title'=>$data['franchisee_name'],//文章id
				'article_keyword'=>$data['franchisee_name'],//文章id
				'article_description'=>$data['franchisee_name'],//文章id
				'article_template'=>'sj_jm_article_show',//文章模板
				'article_time'=>time(),//发布时间
			);
			$article_id = Db::insert('article',$articleData);
			$data['article_id']=$article_id;
			unset($data['franchisee_id']);
			$franchise_id=$franchise->table('franchiser')->insert($data);
			header("location:index.php?act=article&op=article_edit&article_id={$article_id}");
		}
	}

	/**
	 * 编辑加盟商
	 * @return  void
	 */
	public function editOp(){
		$id=isset($_GET['id'])?(int)$_GET['id']:0;
		$franchise=$this->getModelFranchise();
		if (chksubmit()){
			$this->save();
		}elseif($id){
			$info=$franchise->getFranchise($id);
			Tpl::output('data',$info);
			Tpl::showpage('franchise.add');
		}

	}

	/**
	 * 删除加盟商
	 * @return  void
	 */
	public function delOp(){
		$ids=$_POST['del_id'];
		if($ids&&is_array($ids)){
			$franchise=$this->getModelFranchise();
			//查询
			$list=$franchise->table('franchiser')->where(array('franchisee_id'=>array('in',$ids)))->field('franchisee_id,article_id')->select();
			if($list){
				$article_ids=array_column($list,'article_id');
				$franchiser_ids=array_column($list,'franchisee_id');
				Db::delete('article','`article_id` in ('.implode(',',$article_ids).')');
				Db::delete('franchiser','`franchisee_id` in ('.implode(',',$franchiser_ids).')');
				Db::delete('upload','`upload_type`=1 and `item_id` in ('.implode(',',$article_ids).')');
			}
		}
		header("location:index.php?act=franchise&op=index");
	}

	/**
	 * 新增加盟商
	 * @return  void
	 */
	public function addOp(){
		if (chksubmit()){
			$this->save();
		}else{
			Tpl::showpage('franchise.add');
		}

	}
}
